package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.AddressMapper;
import com.pojo.entity.Address;
import com.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : 杨世博
 * @date: Created in 10:52 2023/6/4
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Autowired
    AddressMapper addressMapper;

    @Override
    public List<Address> findAddress(Long uid) {
        QueryWrapper<Address> qw = new QueryWrapper<>();
        qw.eq("uid", uid);
        List<Address> addresses = addressMapper.selectList(qw);
        for (Address address : addresses) {
            address.setAddress(address.getProvince()+address.getCity()+address.getArea()+address.getDetailed());
        }
        return addresses;
    }

    @Override
    public List<Address> addAddress(Address address) {
        addressMapper.insert(address);
        return findAddress(address.getUid());
    }

    @Override
    public List<Address> updateAddress(Address oldAddress) {
        QueryWrapper<Address> qw = new QueryWrapper<>();
        qw.eq("uid",oldAddress.getUid()).eq("add_id",oldAddress.getAddId());
        addressMapper.update(oldAddress, qw);
        return findAddress(oldAddress.getUid());
    }

    @Override
    public List<Address> deleteAddress(long uid, Integer addressId) {
        QueryWrapper<Address> qw = new QueryWrapper<>();
        qw.eq("uid",uid).eq("add_id",addressId);
        addressMapper.delete(qw);
        return findAddress(uid);
    }
}
